package com.syni.mdd.yhd.common.all.utils;

import com.alibaba.fastjson.JSON;
import com.auth0.jwt.JWT;
import com.syni.mdd.sjb.common.component.utils.common.spring.ApplicationContextHolder;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.user.BmsUser;
import com.syni.mdd.yhd.common.all.utils.auth.AuthUserBo;
import com.syni.mdd.yhd.common.all.utils.auth.TokenAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @className TokenUtils
 * @description TOO
 * @Author cfx
 * @DATE 2019/5/5 18:14
 * @VERSION 1.0
 **/
@Component
public class TokenUtils {
    private static TokenAuth tokenAuth;

    public static final int TOKEN_SUCCESS = 1; // 验证成功

    private static ThreadLocal userThreadLocal = new ThreadLocal();

    public static ThreadLocal getUserThreadLocal() {
        return userThreadLocal;
    }

    public static void setUserThreadLocal(ThreadLocal userThreadLocal) {
        TokenUtils.userThreadLocal = userThreadLocal;
    }

    @Autowired
    public void setTokenAuth(TokenAuth tokenAuth) {
        TokenUtils.tokenAuth = tokenAuth;
    }

    /**
     * 生成token,该token长度不一致,如需一致,可自行MD5或者其它方式加密一下
     * 该方式的token只存在磁盘上,如果项目是分布式,最好用redis存储
     * @param str: 该字符串可自定义,在校验token时要保持一致
     * @return
     */
    public static String getToken(String userId,String roleId) {
        return tokenAuth.getToken(userId,roleId);
    }

    public static String getTokenByMinApp(String userId,String roleId){
        return tokenAuth.getToken(userId,roleId, Constants.SMALL_PROGRAM,false);
    }

    public static String getToken(BmsUser bmsUser, String roleId) {
        return tokenAuth.getToken(bmsUser,roleId);
    }

    /**
     * 生成token,该token长度不一致,如需一致,可自行MD5或者其它方式加密一下
     * 该方式的token只存在磁盘上,如果项目是分布式,最好用redis存储
     * @param str: 该字符串可自定义,在校验token时要保持一致
     * @return
     */
    public static String getToken(String userId,String roleId,boolean isRefresh) {
        return tokenAuth.getToken(userId,roleId,isRefresh);
    }

    /**
     * 校验token的有效性
     * @param token
     * @return
     */
    public static int checkToken(String token,String userId) {
        if(StringUtils.isNotBlank(token)) {
            if (ApplicationContextHolder.checkRunProfile(ApplicationContextHolder.RUN_LOCAL)) {
                try {
                    List<String> tokenData = JWT.decode(token).getAudience();
                    String userData = tokenData.get(0);
                    AuthUserBo authUserBo = JSON.parseObject(userData, AuthUserBo.class);
                    TokenUtils.getUserThreadLocal().set(authUserBo.getUserId());
                    if(ApplicationContextHolder.checkRunProfile(ApplicationContextHolder.RUN_DEV)) {
                        //检验Key是否存在
                        return tokenAuth.checkTokenExist(authUserBo);
                    }
                }catch (Exception e){
                    return 0;
                }
                return 1;
            }
            return tokenAuth.checkToken(token, userId);
        }
        return 0;
    }

    public static int checkToken(String token) {
        return checkToken(token,"");
    }

    public static boolean cancellation(String userTokenKey){
        return tokenAuth.cancellation(userTokenKey);
    }

}
